const app = getApp();
import grace from "../../../utils/grace.js";
// socket 连接插件
const io = require('../../../utils/weapp.socket.io.js');
const io_client = io(app.globalData.websocket_url);
const innerAudioContext = wx.createInnerAudioContext();

grace.page({
    data: {
        school_name: '',
        attendance_date: '',
        card_code: '',// 接送卡号
        username: '',// 姓名
        class_id: 0,
        class_name: '',// 班级
        pic_url: '',// 幼儿头像
        avatar: '',// 拍照头像
        attendances: [],
        attendances_temp: [],
        get_focus: true
    },
    onLoad () {
        let that = this;
        let school_id = wx.getStorageSync('school_id')
        let d = new Date();
        that.$data = {
            'school_name': wx.getStorageSync('school_name'),
            'class_id': wx.getStorageSync('class_id'),
            'class_name': wx.getStorageSync('class_name'),
            'attendance_date': d.getFullYear() + '年' + (d.getMonth() + 1) + "月" + d.getDate() + '日'
        }

        io_client.on('connect', function() {
            // 连接成功时的事件
            io_client.emit('login', {username: wx.getStorageSync('partner_id')});
        });

        io_client.on('disconnect', function() {
            // 断开连接时的事件
            io_client.emit('logout', {username: wx.getStorageSync('partner_id')});
        });

        io_client.on("clock", function(resp){
            // 接收到服务器端发来的打卡事件
            if(resp['card_flag'] == 'student'){
                // 打卡信息显示在页面上
                that.$data.attendances_temp.push(resp);
                that.$data.attendances = that.$data.attendances_temp;
                that.$data.attendances.reverse();

                // 语音播报
                innerAudioContext.src = app.globalData.audio_url + 'clock_' + resp['class_id'] + '_' + resp['student_id'] + '.mp3';
                innerAudioContext.play();

                // 离开接送卡对应幼儿所在班级
                io_client.emit('leave', {school_id: school_id, username: wx.getStorageSync('partner_id'), room: resp['class_id']});
            }else if (resp['card_flag'] == 'draft'){
                // 语音播报
                innerAudioContext.src = app.globalData.audio_url + 'clock_card_draft.mp3';
                innerAudioContext.play();
            }else if (resp['card_flag'] == 'cancel'){
                // 语音播报
                innerAudioContext.src = app.globalData.audio_url + 'clock_card_cancel.mp3';
                innerAudioContext.play();
            }else if (resp['card_flag'] == 'none'){
                // 语音播报
                innerAudioContext.src = app.globalData.audio_url + 'clock_card_none.mp3';
                innerAudioContext.play();
            }
        });
    },
    clock(e){
        let that = this;
        let school_id = wx.getStorageSync('school_id')
        if(e.detail.value.length == 10) {
            // 打卡
            io_client.emit('clock', {school_id: school_id, terminal: wx.getStorageSync('partner_id'), card_code: e.detail.value});
            that.$data.card_code = '';

            // 拍照
            let ctx = wx.createCameraContext();
            ctx.takePhoto({
                quality: 'high',
                success: (res) => {
                    that.$data.avatar = res.tempImagePath;
                }
            })
        }
    }
})